package basis.第十四章;


import org.junit.experimental.theories.Theories;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
class Immutable{
    private int value = 0;
    public Immutable(int value){
        this.value = value;
    }
    public int getValue(){
        return this.value;
    }
    public Immutable add(int v){
        return new Immutable(this.value + v);
    }

    public Immutable sub(int v){
        return new Immutable(this.value - v);
    }
}

class Immutable2{
    private int value = 0;
    public Immutable2(int value){
        this.value = value;
    }
    public int getValue(){
        return this.value;
    }
    public void add(int v){
         this.value += v;
    }

    public void sub(int v){
       this.value -= v;
    }
}
public class Example5 {
    public static void main(String[] args)throws Exception {
        Immutable immutable = new Immutable(0);
        Immutable2 immutable2 = new Immutable2(0);
        Thread t1 = new Thread(()->{
            for(int i=0;i<10000;i++) {
                immutable.add(1);
            }

        },"t1");

        Thread t2 = new Thread(()->{
            for(int i=0;i<10000;i++)
                immutable.sub(1);
        },"t2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(immutable.getValue());
    }



}
